package com.hmall.order.service.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.common.client.ItemClient;
import com.hmall.common.client.UserClient;
import com.hmall.common.dto.Address;
import com.hmall.common.dto.Item;
import com.hmall.common.dto.User;
import com.hmall.common.utils.BaseContext;
import com.hmall.order.builder.AllOrderData;
import com.hmall.order.builder.AllOrderDataBuilder;
import com.hmall.order.mapper.OrderMapper;
import com.hmall.order.pojo.Order;
import com.hmall.order.pojo.OrderDetail;
import com.hmall.order.pojo.OrderLogistics;
import com.hmall.order.pojo.OrderParams;
import com.hmall.order.service.IOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.UUID;

@Service
@Slf4j
public class OrderService extends ServiceImpl<OrderMapper, Order> implements IOrderService {
    @Autowired
    private ItemClient itemClient;
    @Autowired
    private UserClient userClient;
    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private OrderLogisticsService orderLogisticsService;

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Override
    @Transactional
    public Long order(OrderParams params) {

        //订单详情
        AllOrderDataBuilder dataBuilder = new AllOrderDataBuilder(itemClient, userClient, orderDetailService, orderLogisticsService, rabbitTemplate, params);
        //构建订单相关数据
        AllOrderData build = dataBuilder
                .buildOrder()
                .buildOrderDetails()
                .buildOrderLogistics()
                .build();
        //物流地址
        Order order = build.getOrder();
        OrderDetail orderDetail = build.getOrderDetail();
        OrderLogistics orderLogistics = build.getOrderLogistics();
        Long totalFee = order.getTotalFee();

        Long userId = BaseContext.getCurrentThread();

        User user = userClient.getUserById(userId);

        user.setBalance(user.getBalance() - totalFee);

        Item item = itemClient.queryItemById(params.getItemId());


        itemClient.updateStock(item.getId(), params.getNum());
        userClient.updateUser(user);
        //保存到数据库
        save(order);

        orderDetailService.save(orderDetail);
        orderLogisticsService.save(orderLogistics);

        log.info("order: {}", order);
        log.info("orderDetail: {}", orderDetail);
        return order.getId();
    }

    @Override
    public void sendMessage(Long order) {
        Message message = MessageBuilder
                .withBody(order.toString().getBytes(StandardCharsets.UTF_8))
                .setHeader("x-delay", 30000)
                .build();

        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
        rabbitTemplate.convertAndSend("delay.order", "delay", message, correlationData);
        log.info("发送延迟消息: {}", order);
    }

}
